/**
 * Created by WYH on 17/4/23.
 */
angular.module('lockFillIn', []).component(
    'lockFillIn',
    {
        templateUrl: 'component/lock/lockFillIn/lock_fill_in.template.html',
        controller: [
            '$rootScope', '$scope', 'LockModel', 'Api', 'EleStyle','Alert',
            function ($rootScope, $scope, LockModel, Api, EleStyle,Alert) {

                getCompanyManagerList();

                getCompanyInfo();

                $scope.EleStyle = EleStyle;


                $scope.submit = submit;
                $scope.time = [];

                $scope.onTimeSetEnd = function (date, oldDate) {
                    var activeTime = $scope.lockModel.activityTime;
                    var time = $scope.time;
                    $scope.time = [time[0], date]
                    $scope.lockModel.activityTime = [activeTime[0], getSecond(date)];
                    $scope.showEndTimeSelect = false;
                };

                $scope.onTimeSetStart = function (newDate, oldDate) {
                    var activeTime = $scope.lockModel.activityTime;
                    var time = $scope.time;
                    $scope.time = [newDate, time[1]]
                    $scope.lockModel.activityTime = [getSecond(newDate), activeTime[1]];
                    $scope.showStartTimeSelect = false;
                };

                $scope.$parent.$watch('modifyLock', function (newLock) {
                    var newLockModel = newLock ? new LockModel(newLock.getAllObject()) : new LockModel();
                    $scope.lockModel = newLockModel;
                    $scope.lockModel.authType = newLock ? newLockModel.authType : 'GENERIC';
                    $scope.lockModel.actived = newLock ? newLockModel.actived : false;
                    $scope.lockModel.manager = {
                        id: newLockModel.managerId,
                        name: newLockModel.managerName
                    };
                });

                function submit() {

                    if (!checkLockNull()) {
                        return Alert.alertBox('请检查信息是否输入完整')
                    }
                    if (!!$scope.$parent.modifyLock) {
                        callModifyLockService();
                    } else {
                        callAddLockServie();
                    }
                }

                function checkLockNull() {
                    var manager = $scope.lockModel.manager.name;
                    var startTime = $scope.lockModel.getStartTimeStr();
                    var endTime = $scope.lockModel.getEndTimeStr();
                    var name = $scope.lockModel.name;
                    var desc = $scope.lockModel.desc;
                    var controlId=$scope.lockModel.controlId;
                    var controlKey=$scope.lockModel.controlKey;
                    var snr=$scope.lockModel.snr;
                    if (!startTime || !endTime || !name || !desc ||!manager||!controlId||!controlKey) {
                        return false
                    } else {
                        return true
                    }
                }

                function callAddLockServie() {
                    $scope.lockModel.managerId = $scope.lockModel.manager.id;
                    if (!$scope.lockModel.manager) {
                        Alert.alertBox('未选择管理员');
                    } else {
                        Api.createLock($scope.lockModel, function (result) {
                            Alert.alertBox('添加成功',function () {
                              return window.location.reload();
                            });
                        });
                    }

                }

                function callModifyLockService() {

                    if (!$scope.lockModel.manager) {
                        Alert.alertBox('未选择管理员');
                    } else {
                        $scope.lockModel.managerName = $scope.lockModel.manager.name;
                        $scope.lockModel.managerId = $scope.lockModel.manager.id;
                        Api.modifyLock($scope.lockModel, function (result) {
                            console.log($scope.lockModel);
                            console.log(result);
                            Alert.alertBox('修改成功',function () {
                                return window.location.reload();
                            });
                        });
                    }

                }



                function getCompanyManagerList() {
                    Api.getCompanyMemberList(null, function (result) {
                        if (result.data.length == 0) {
                            Alert.alertBox('提示：该公司还未设置管理员');
                        } else {
                            $scope.managerList = result.data;
                        }
                    });
                }


                // function setupManager(list) {
                //     list.forEach(function () {
                //
                //     })
                // }


                function getCompanyInfo() {
                    $rootScope.$watch('RootCompanyInfo', function (newCompanyInfo) {
                        if (newCompanyInfo) {
                            $scope.companyDetail = newCompanyInfo;
                        }
                    })
                }


                function getSecond(date) {
                    var new_date = new Date(date.getTime())
                    var str = new_date.getFullYear() + '/' + (new_date.getMonth() + 1) + '/' + new_date.getDate();
                    var timestamp = Date.parse(str);
                    var current = new_date;
                    var unixTimestamp = Date.parse(str) / 1000;
                     if(new_date.getMinutes() == 59){
                        current.setHours(current.getHours()+1)
                        current.setMinutes(0,0,0)
                    }
                    return current.getTime() / 1000 - unixTimestamp;
                }

                Date.prototype.intHour=function (){
                    this.setMinutes(0,0,0)
                };
                Date.prototype.intMinutes=function (){
                    this.setSeconds(0,0)
                };
                $scope.beginDateBeforeRender = function ($views,$dates) {

                    var start_time = $scope.time[1] || new Date().setHours(23, 59, 59, 59);

                    var todaySinceMidnight = new Date(start_time);
                    var currentTime = new Date(start_time);
                    currentTime.setHours(currentTime.getHours()+8)

                    todaySinceMidnight.setHours(todaySinceMidnight.getHours()+8)


                    if($views=='hour'){
                        todaySinceMidnight.intHour()
                    }
                    if($views=='minute'){
                        todaySinceMidnight.intMinutes()
                    }
                    $dates.filter(function (date) {
                        if($views == 'minute'){
                            return date.utcDateValue >= todaySinceMidnight.getTime();
                        }
                        return date.utcDateValue > todaySinceMidnight.getTime()||date.utcDateValue==currentTime.getTime();
                    }).forEach(function (date) {
                        date.selectable = false;
                    });
                    $dates.filter(function (date) {
                        if($views == 'minute'){
                            return date.utcDateValue < todaySinceMidnight.getTime();
                        }
                        return date.utcDateValue <= todaySinceMidnight.getTime()&&date.utcDateValue!=currentTime.getTime();
                    }).forEach(function (date) {
                        date.selectable = true;
                    });
                };


                $scope.endDateBeforeRender = function ($views,$dates) {

                    var end_time = $scope.time[0];

                    var todaySinceMidnight = new Date(end_time);
                    todaySinceMidnight.setHours(todaySinceMidnight.getHours()+8)
                    var currentTime = new Date(end_time);
                    currentTime.setHours(currentTime.getHours()+8)

                    if($views=='hour'){
                        todaySinceMidnight.intHour()
                    }
                    if($views=='minute'){
                        todaySinceMidnight.intMinutes()
                    }
                    $dates.filter(function (date) {
                        if($views == 'minute'){
                            return date.utcDateValue <= todaySinceMidnight.getTime();
                        }
                        return date.utcDateValue < todaySinceMidnight.getTime()||date.utcDateValue+55*60*1000 ==currentTime.getTime() ;
                    }).forEach(function (date) {

                        date.selectable = false;
                    });
                    $dates.filter(function (date) {
                        if($views == 'minute'){
                            return date.utcDateValue >todaySinceMidnight.getTime();
                        }
                        return date.utcDateValue >= todaySinceMidnight.getTime()&& date.utcDateValue+55*60*1000!= currentTime.getTime();
                    }).forEach(function (date) {
                        date.selectable = true;
                    });
                };


            }
        ]
    }
);